package org.light.simpleauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.TableStringUtil;
import org.light.utils.WriteableUtil;

public class GetSaltUser extends Verb implements EasyUIPositions {
	
	protected String generateMariaDBGetSaltSqlStatement() throws Exception{
		return "select "+domain.findFieldByFixedName("salt").getFeildNameAsTableColumn()+ " from "+ domain.getDbPrefix() +TableStringUtil.domainNametoTableName(domain) +" where "+domain.findFieldByFixedName("userName").getFeildNameAsTableColumn() + " = ?";
	}
	
	protected String generatePgsqlGetSaltSqlStatement() throws Exception{
		return "select "+domain.findFieldByFixedName("salt").getFeildNameAsTableColumn()+ " from "+ domain.getDbPrefix() +TableStringUtil.domainNametoTableName(domain) +" where "+domain.findFieldByFixedName("userName").getFeildNameAsTableColumn() + " = $1";
	}
	
	protected String generateOracleGetSaltSqlStatement() throws Exception{
		return "select "+domain.findFieldByFixedName("salt").getFeildNameAsTableColumn()+ " from "+ domain.getDbPrefix() +TableStringUtil.domainNametoTableName(domain) +" where "+domain.findFieldByFixedName("userName").getFeildNameAsTableColumn() + " = :"+domain.findFieldByFixedName("userName").getLowerFirstFieldName();
	}
	
	@Override
	public Method generateDaoImplMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("GetSalt"+StringUtil.capFirst(this.domain.getStandardName()));		
			method.addSignature(new Signature(1, "db","*sql.DB"));
			method.addSignature(new Signature(2, this.domain.getDomainName().getLowerFirstFieldName(),this.domain.getDomainName().getFieldType()));
			method.setReturnType(new Type("string"));	
			
			List<Writeable> sList = new ArrayList<Writeable>();
			
			if (StringUtil.isBlank(this.getDbType())||this.getDbType().equalsIgnoreCase("MariaDB")||this.getDbType().equalsIgnoreCase("MySQL")) {
				sList.add(new Statement(1000L,1,"sql := \""+generateMariaDBGetSaltSqlStatement()+"\""));
			}else if (this.getDbType().equalsIgnoreCase("PostgreSQL")||this.getDbType().equalsIgnoreCase("pgsql")) {
				sList.add(new Statement(1000L,1,"sql := \""+generatePgsqlGetSaltSqlStatement()+"\""));
			}else if (this.getDbType().equalsIgnoreCase("Oracle")) {
				sList.add(new Statement(1000L,1,"sql := \""+generateOracleGetSaltSqlStatement()+"\""));
			}

			sList.add(new Statement(2000L,1,"stmt, err := db.Prepare(sql)"));
			sList.add(new Statement(3000L,1,"defer stmt.Close()"));
			sList.add(new Statement(4000L,1,"if err != nil {"));
			sList.add(new Statement(5000L,2,"panic(err)"));
			sList.add(new Statement(6000L,1,"}"));
			sList.add(new Statement(7000L,1,"row := stmt.QueryRow("+this.domain.getDomainName().getLowerFirstFieldName()+")"));
			sList.add(new Statement(8000L,1,"var secondSecret NullString"));
			sList.add(new Statement(9000L,0,""));
			sList.add(new Statement(10000L,1,"err = row.Scan(&secondSecret)"));
			sList.add(new Statement(11000L,1,"if err != nil {"));
			sList.add(new Statement(12000L,2,"panic(err)"));
			sList.add(new Statement(13000L,1,"}"));
			sList.add(new Statement(14000L,1,"return secondSecret.String"));

			
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			return method;
		}
	}
	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			return method;
		}
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}
	
	public GetSaltUser(){
		super();
		this.setLabel("获取用户盐");
	}
	
	public GetSaltUser(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("GetSaltUser");
		this.setVerbName("GetSalt"+StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("获取用户盐");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("GetSaltUser");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		return null;		
	}
	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;		
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}
}
